package etc;


import java.io.File;
import javax.swing.JFileChooser;


/**
 * Rewritten from sratch for more or less obvious reasons. Nothing personal :)
 * @author Zoli
 */
public class FileSelectionClass extends JFileChooser
{

    private File file;
    private boolean cancelled;

    /**
     * Inits properties, sets file filter.
     */
    public FileSelectionClass()
    {
        super();
        this.setMultiSelectionEnabled(false);
        this.cancelled=false;
        this.file=null;

    }




    /**
     * Displays an open dialog and sets the selected file.
     */
    public void open()
    {
        if (this.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
        {
            this.cancelled=false;
            this.file=this.getSelectedFile();
        }
        else
        {
            this.cancelled=true;
        }
    }

    public boolean wascancelled()
    {
        return this.cancelled;
    }

    /**
     * Returnes the file which was selected. In case no file was selected it throws an exception.
     * @return The selected path_to_selected_file/selected_file.
     * @throws NoFileSelectedException
     */
    public File getSelectedDocument() throws NoFileSelectedException
    {
        if (this.file==null)
        {
            throw new NoFileSelectedException("No file was selected.");
        }
        else
        {
            return this.file;
        }

    }

    /**
     * Displays a save dialog which, and then sets the instance variables.
     */
    public void save()
    {
        if (this.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
        {
            this.cancelled=false;
            this.file=this.getSelectedFile();
        }
        else
        {
            this.cancelled=true;
        }
    }


}